package com.quanyan.place.web.api;

import java.math.BigDecimal;
import java.util.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import com.quanyan.orderpay.request.ReqPayInfo;
import com.quanyan.orderpay.response.RespPreOrder;
import com.quanyan.place.entity.apireq.ReqQRCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.place.constants.PlaceConstants;
import com.quanyan.place.entity.apireq.ReqPlaceTicketInfo;
import com.quanyan.place.mapper.PosUnionPayMapper;
import com.quanyan.place.service.ApiPlaceTicketService;

/**
 * Created by zhaohui on 2016/10/12.
 */
@RestController
@RequestMapping("/api/place/ticket")
public class ApiPlaceTicketController {

	@Autowired
	ApiPlaceTicketService apiPlaceTicketService;

	@Autowired
	PosUnionPayMapper posUnionPayMapper;

	/**
	 * 购买门票
	 * 
	 * @param reqPlaceTicketInfo
	 * @return
	 */
	@RequestMapping(value = "/placeTicketInfo", method = RequestMethod.POST, produces = {
			"application/json; charset=UTF-8" })
	public APIResponse<?> placeTicketInfo(@RequestBody @Valid ReqPlaceTicketInfo reqPlaceTicketInfo,
			BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
		}
		return apiPlaceTicketService.placeTicketInfo(reqPlaceTicketInfo);
	}

	/**
	 * 支付
	 * 
	 * @param reqPlaceTicketInfo
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(value = "/pay")
	public APIResponse<?> pay(@RequestBody @Valid ReqPlaceTicketInfo reqPlaceTicketInfo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        boolean result = false;
        // 判断是否为会员卡消费
        List<ReqPayInfo> reqPayInfos = reqPlaceTicketInfo.getPayInfos();
        Iterator<ReqPayInfo> iter = reqPayInfos.iterator();
        while (iter.hasNext()) {
            ReqPayInfo payInfo = iter.next();
            if (PlaceConstants.PAY_WAY_FOR_CARD.equals(payInfo.getPayId())) {
                result = true;
                break;
            }
        }
        if (result){
            if (null == reqPlaceTicketInfo.getCardNo() || org.springframework.util.StringUtils.isEmpty(reqPlaceTicketInfo.getCardNo())) {
               return APIResponse.returnFail("会员卡不能为空");
            }
            return apiPlaceTicketService.cardPay(reqPlaceTicketInfo);
        }else{
            if (null != reqPlaceTicketInfo.getCardNo() || !org.springframework.util.StringUtils.isEmpty(reqPlaceTicketInfo.getCardNo())){
                return apiPlaceTicketService.cardPay(reqPlaceTicketInfo);
            }
            return apiPlaceTicketService.pay(reqPlaceTicketInfo);

        }
       //return APIResponse.returnFail("门票支付异常!");

    }



}
